package com.qf.ability.auth.service.auth;

import com.qf.ability.auth.entity.AuthInfo;
import com.qf.ability.auth.service.IAuthService;
import com.qf.business.user.feign.SysUserFeign;
import com.qf.commons.core.exception.ServiceException;
import com.qf.commons.data.base.BaseUser;
import com.qf.commons.data.result.R;
import com.qf.data.user.dto.SysUserPowersDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 后台系统登录的业务流程
 */
@Service("authService0")
@Slf4j
public class SysAuthServiceImpl implements IAuthService {

    @Autowired
    private SysUserFeign sysUserFeign;

    //处理后台系统登录的逻辑
    @Override
    public BaseUser login(AuthInfo authInfo) {
        //通过用户名查询用户信息以及权限信息
        R<SysUserPowersDto> result = sysUserFeign.queryUserByUnAndPw(authInfo.getUsername());
        SysUserPowersDto userPower = result.getData();
        log.debug("[auth user] 登录认证的结果 - {}", userPower);

        //校验密码
        if (userPower == null || !authInfo.getPassword().equals(userPower.getPassword())) {
            throw new ServiceException(500, "用户名或者密码不正确");
        }
        //密码置空
        userPower.setPassword(null);

        return userPower;
    }
}